package org.test.demo.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by Hy on 2021/7/25.
 * 导出运行时生成的动态代理对象到文件方法：在启动类的VM options中添加如下参数
    方法1：-Djdk.proxy.ProxyGenerator.saveGeneratedFiles=true -XX:+TraceClassLoading
    方法2：-Dsun.misc.ProxyGenerator.saveGeneratedFiles=true（我的IDEA2017用方法2有效）
    方法3：在代码中加入System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");（我的IDEA2017用方法3有效）
 */
public class AnnotationParser {
    //解析类的注解
    public static void parseTypeAnnotation() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("org.test.demo.annotation.ImoocCourse");
        Annotation[] annotations = clazz.getAnnotations();
        for (Annotation annotation:annotations
             ) {
            CourseInfoAnnotation courseInfoAnnotation = (CourseInfoAnnotation) annotation;
            System.out.println("课程名:"+courseInfoAnnotation.courseName() + "\n"+
            "课程标签:"+courseInfoAnnotation.courseTag()+"\n"+
            "课程简介:"+courseInfoAnnotation.courseProfile()+"\n"+
            "课程序号:"+courseInfoAnnotation.courseIndex());
        }
    }

    //解析成员变量上的标签
    public static void parseFieldAnnotation() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("org.test.demo.annotation.ImoocCourse");
        Field[] fields = clazz.getDeclaredFields();
        for (Field f:fields
             ) {
            //判断成员变量中是否有指定注解类型的注解
            boolean hasAnn = f.isAnnotationPresent(PersonInfoAnnotation.class);
            if(hasAnn){
                PersonInfoAnnotation infoAnnotation = f.getAnnotation(PersonInfoAnnotation.class);
                System.out.println("名字:"+infoAnnotation.name() +"\n"+
                "年龄:"+infoAnnotation.gender()+"\n"+
                "性别:"+infoAnnotation.gender());
                for (String language:infoAnnotation.language()
                     ) {
                    System.out.println("课程名:"+language);
                }
            }
        }
    }
    
    public static void parseMethodAnno() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("org.test.demo.annotation.ImoocCourse");
        Method[] methods = clazz.getDeclaredMethods();
        for (Method me : methods) {
            boolean hasAnno = me.isAnnotationPresent(CourseInfoAnnotation.class);
            if(hasAnno){
                CourseInfoAnnotation annotation = me.getAnnotation(CourseInfoAnnotation.class);
                System.out.println("课程名:"+annotation.courseName()+"\n"+
                "课程标签:"+annotation.courseTag()+"\n"+
                "课程简介:"+annotation.courseProfile()+"\n"+
                "课程序号:"+annotation.courseIndex());
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        //System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        //parseTypeAnnotation();
        parseFieldAnnotation();
        //parseMethodAnno();
    }
}
